home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gptx-0_2.lha / gptx-0.2 / lib / bumpalloc.h < prev    next >
Text File  |  1991-10-09  |  2KB  |  47 lines

  1. /* BUMP_ALLOC macro - increase table allocation by one element.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Francois Pinard <pinard@iro.umontreal.ca>, 1990.
  4. */
  5.  
  6.  
  7. /* Bump the allocation of the array pointed to by TABLE whenever required.
  8.    The table already has already COUNT elements in it, this macro insure it
  9.    has enough space to accomodate at least one more element.  Space is
  10.    allocated (2 ^ EXPONENT) elements at a time.  Each element of the array
  11.    is of type TYPE.
  12.  
  13.    Routines `xmalloc' and `xrealloc' are called to do the actual memory
  14.    management.  This implies that the program will abort with an `Out of
  15.    memory!' error if any problem arise.
  16.  
  17.    To work correctly, at least EXPONENT and TYPE should always be the same
  18.    for all uses of this macro for any given TABLE.  A secure way to achieve
  19.    this is to never use this macro directly, but use it to define other
  20.    macros, which would then be TABLE-specific.
  21.  
  22.    The first time through, COUNT is usually zero.  Note that COUNT is not
  23.    updated by this macro, but it should be udpate elsewhere, later.  This is
  24.    convenient, because it allows TABLE[COUNT] to refer to the new element at
  25.    the end.  Once its construction is completed, COUNT++ will record it in
  26.    the table.  Calling this macro several times in a row without updating
  27.    COUNT is a bad thing to do.  */
  28.  
  29. #define BUMP_ALLOC(table, count, exponent, type) \
  30.   BUMP_ALLOC_VARSIZE ((table), (count), (exponent), type, sizeof (type))
  31.  
  32.  
  33. /* In cases `sizeof TYPE' would not always yield the correct value for the
  34.    size of each element entry, this macro accepts a supplementary SIZE
  35.    argument.  The EXPONENT, TYPE and SIZE parameters should still have the
  36.    same value for all macro calls related to a specific TABLE.  */
  37.  
  38. #define BUMP_ALLOC_VARSIZE(table, count, exponent, type, size) \
  39.   if (((count) & ((1 << (exponent)) - 1)) == 0)                \
  40.     if ((count) == 0)                            \
  41.       (table) = (type *) xmalloc ((1 << exponent) * size);        \
  42.     else                                \
  43.       (table)                                \
  44.     = (type *) xrealloc ((table),                    \
  45.                  ((count) + (1 << (exponent))) * size);    \
  46.   else
  47.